home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / vc / pro7 / ftoa.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-28  |  4.3 KB  |  100 lines

  1. /* ftoa.c */
  2.  
  3. char * _cdecl _fcvt(double, int, int *, int *);
  4.  
  5. static int nDec, nSign;
  6.  
  7. /************************************************************************
  8. * FloatToString:                                                        *
  9. * This function converts floating point numbers to strings. It returns  *
  10. * a pointer to a string containing the converted number.                *
  11. *                                                                       *
  12. * Parameter             Type/Description                                *
  13. *                                                                       *
  14. * lfNumber              double Contains the number to be converted.     *
  15. *                                                                       *
  16. * szBuffer              LPSTR Points to a buffer which will recieve the *
  17. *                       string equivalent of the number.                *
  18. *                                                                       *
  19. * wPlaces               WORD Contains the number of decimal places the  *
  20. *                       number should be converted to.                  *
  21. *                                                                       *
  22. ************************************************************************/
  23.  
  24. LPSTR WINAPI FloatToString ( double lfNumber, LPSTR lpszBuffer, WORD wPlaces )
  25. {
  26.       /******************************************************************
  27.       * Local Variables:                                                *
  28.       * LPSTR lpNum is used to store the address of the converted       *
  29.       * number returned by _fcvt.                                       *
  30.       ******************************************************************/
  31.  
  32.       LPSTR lpNum;
  33.  
  34.       /******************************************************************
  35.       * Convert Number:                                                 *
  36.       * _fcvt is called to convert the number into a string. The return *
  37.       * value is stored in lpNum. The storage buffer is zeroed.         *
  38.       ******************************************************************/
  39.  
  40.       lpNum=(LPSTR)_fcvt ( lfNumber, wPlaces, &nDec, &nSign );
  41.       *lpszBuffer=EOS;
  42.  
  43.       /******************************************************************
  44.       * Put In Minus Sign:                                              *
  45.       * If nSign is non_zero then the number is negative and a minus    *
  46.       * sign is put at the start of the return string.                  *
  47.       ******************************************************************/
  48.  
  49.       if ( nSign )
  50.             lstrcat ( lpszBuffer, "-" );
  51.  
  52.       /******************************************************************
  53.       * Process Numbers:                                                *
  54.       * If nDec is less than or equal to zero then the number converted *
  55.       * is less than one. A zero and a decimal point are added to the   *
  56.       * return string and then as many zeros as necessary are also      *
  57.       * added. The number string is then added.                         *
  58.       * If nDec is greater than zero then the _fcvt string is copied    *
  59.       * into the return string upto the nDec'th character. A decimal    *
  60.       * point is then added and the remainder of the _fcvt string is    *
  61.       * added.                                                          *
  62.       * ftoa then returns lpszBuffer, the address of the start of the   *
  63.       * return string.                                                  *
  64.       ******************************************************************/
  65.  
  66.       if ( nDec<=0 )
  67.       {
  68.             lstrcat ( lpszBuffer, "0." );
  69.  
  70.             for (;nDec++;)
  71.                   lstrcat ( lpszBuffer, "0" );
  72.  
  73.             lstrcat ( lpszBuffer, lpNum );
  74.       }
  75.       else
  76.       {
  77.             lstrncat ( lpszBuffer, lpNum, (WORD)nDec );
  78.             lstrcat ( lpszBuffer, "." );
  79.             lstrcat ( lpszBuffer, lpNum+nDec );
  80.       }
  81.  
  82.       return lpszBuffer;
  83. }
  84.  
  85.  
  86. LPSTR WINAPI lstrncat ( LPSTR lpszDest,
  87.                         LPSTR lpszSrc,
  88.                         WORD wChars )
  89. {
  90.       LPSTR lpszRet=lpszDest;
  91.  
  92.       lpszDest+=lstrlen ( lpszDest );
  93.  
  94.       for (;*lpszSrc && wChars;wChars--)
  95.             *lpszDest++=*lpszSrc++;
  96.  
  97.       *lpszDest=EOS;
  98.       return lpszRet;
  99. }
  100.